<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use app\common\controller\AdminBaseController;
class AuthController extends AdminBaseController
{
	
	 protected function _initialize() {
        parent::_initialize();
        $store_menu_list       = Db::name('auth_rule') ->order(['sort' => 'ASC', 'id' => 'ASC'])->select();
        $store_menu_level_list = array2level($store_menu_list);
        $this->assign('store_menu_level_list', $store_menu_level_list);
        
    }
    
	public function index(){
        return $this->fetch();
    }
	
	/*
	 
	 * 添加菜单
	 * 
	 * */
	
	
	public function add_rule(){
		if(input('post.')){
            $param = input('post.');
            $param['add_time']=now_time();
            if(Db::name('auth_rule')->insert($param)){
            	$text='【'.session('admin_name').'】添加'.$param['title'].'菜单';
            	$status='操作成功';
            	logc($text,$status);
            	return	toSuccess('添加成功');
           	 	}else{
            	return toError(408,'添加失败，请重新添加');
            }
            
        }

        return toError(408,'添加失败，请重新添加');
		
	}
	

    /**
     * [edit_rule 编辑菜单]
     * @return [type] [description]
     */
    public function edit_rule()
    {
        if(input('post.')){
            $param = input('post.');
            $d=Db::name('auth_rule')->where('id',input('id'))->find();
            if(Db::name('auth_rule')->update($param)){
            	$text='【'.session('admin_name').'】修改'.$d['title'];
            	$status='操作成功';
            	logc($text,$status);
            	return	toSuccess('修改成功');
            }else{
            	 return toError(408,'修改失败，请重新修改');
            }
        }
        $id = input('param.id');
        $this->assign('menu',Db::name('auth_rule')->find($id));
        return $this->fetch();
    }

	
	

    /**
     * [roleDel 删除菜单]
     * @return [type] [description]
     */
    public function del_rule()
    {
        $id = input('param.id');
        $d=Db::name('auth_rule')->where('id',input('id'))->find();
        if (Db::name('auth_rule')->where('id',$id)->delete()) {
        	$text='【'.session('admin_name').'】删除'.$d['title'].'菜单';
            	$status='操作成功';
            	logc($text,$status);
           return	toSuccess('删除成功');
        } else {
            return toError(408,'删除失败，请重新删除');
        }
    }
    
    
    
        /**
     * [rule_state 菜单状态]
     * @return [type] [description]
     */
    public function rule_state()
    {
        $id = input('id');
        $status = Db::name('auth_rule')->where('id',$id)->value('status');//判断当前状态
        $d=Db::name('auth_rule')->where('id',$id)->find();
        if($status==1)
        {
            if(Db::name('auth_rule')->where('id',$id)->update(['status'=>0])){
            	$text='【'.session('admin_name').'】禁用'.$d['title'].'菜单';
            	$status='操作成功';
            	logc($text,$status);
            	return	toSuccess('已禁用');
            }else{
            	 return toError(408,'更换失败');
            }

        }
        else
        {
            if(Db::name('auth_rule')->where('id',$id)->update(['status'=>1])){
            	$text='【'.session('admin_name').'】开启'.$d['title'].'菜单';
            	$status='操作成功';
            	logc($text,$status);
            	return	toSuccess('开启中');
            }else{
            	 return toError(408,'更换失败');
            }
        }
    
    }
}
?>